const Sequelize = require('sequelize');

module.exports = {
  init: (sequelize) => {
    // 用户模型
    const User = sequelize.define('User', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
      },
      username: {
        type: Sequelize.STRING(50),
        unique: true,
        allowNull: false
      },
      password: {
        type: Sequelize.STRING(100),
        allowNull: false
      },
      role: {
        type: Sequelize.STRING(50),
        allowNull: false,
        defaultValue: 'user',
        validate: {
          isIn: [['admin', 'user']]
        }
      },
      theme: {
        type: Sequelize.STRING(20),
        defaultValue: 'light'
      },
      created_at: {
        type: Sequelize.DATE,
        defaultValue: Sequelize.NOW
      }
    });

    // 设备分组模型
    const DeviceGroup = sequelize.define('DeviceGroup', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
      },
      user_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'Users', // 关联 User 表
          key: 'id'      // 关联 User.id 字段
        },
        onDelete: 'CASCADE'
      },
      group_name: {
        type: Sequelize.STRING(50),
        allowNull: false
      }
    });

    // 设备模型
    const Device = sequelize.define('Device', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
      },
      name: {
        type: Sequelize.STRING(50),
        allowNull: false
      },
      type: {
        type: Sequelize.STRING(50),
        allowNull: false,
        validate: {
          isIn: [['light', 'thermostat', 'security']]
        }
      },
      status: {
        type: Sequelize.JSON,
        allowNull: false
      },
      created_at: {
        type: Sequelize.DATE,
        defaultValue: Sequelize.NOW
      }
    });

    // 操作日志模型
    const OperationLog = sequelize.define('OperationLog', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
      },
      device_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'Devices', // 关联 Device 表
          key: 'id'        // 关联 Device.id 字段
        }
      },
      action: {
        type: Sequelize.STRING(50),
        allowNull: false
      },
      detail: {
        type: Sequelize.JSON
      },
      created_at: {
        type: Sequelize.DATE,
        defaultValue: Sequelize.NOW
      }
    });

    // 设备数据模型
    const DeviceData = sequelize.define('DeviceData', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
      },
      device_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'Devices', // 关联 Device 表
          key: 'id'        // 关联 Device.id 字段
        }
      },
      param_type: {
        type: Sequelize.STRING(50),
        allowNull: false,
        validate: {
          isIn: [['energy', 'temperature', 'humidity']]
        }
      },
      value: {
        type: Sequelize.DECIMAL(10, 2),
        allowNull: false
      },
      recorded_at: {
        type: Sequelize.DATE,
        defaultValue: Sequelize.NOW
      }
    });

    // 权限模型
    const Permission = sequelize.define('Permission', {
      role: {
        type: Sequelize.STRING(50),
        primaryKey: true,
        validate: {
          isIn: [['admin', 'user']]
        }
      },
      can_add_device: {
        type: Sequelize.INTEGER,
        defaultValue: 0
      },
      can_delete_device: {
        type: Sequelize.INTEGER,
        defaultValue: 0
      },
      can_control_device: {
        type: Sequelize.INTEGER,
        defaultValue: 1
      },
      can_manage_users: {
        type: Sequelize.INTEGER,
        defaultValue: 0
      },
      can_view_logs: {
        type: Sequelize.INTEGER,
        defaultValue: 0
      },
      updated_at: {
        type: Sequelize.DATE,
        defaultValue: Sequelize.NOW,
        onUpdate: Sequelize.NOW
      }
    });

    // 同步数据库（开发环境使用）
    sequelize.sync({ force: false });

    // 返回模型
    return {
      User,
      DeviceGroup,
      Device,
      OperationLog,
      DeviceData,
      Permission
    };
  }
};